package xj.graph2d.app;

import java.util.ArrayList;
import java.util.List;

import xj.graph2d.Document;

/**
 * Manage mutiple documents, sharing the same view.
 */
public class DocumentManager {

  public DocumentManager(DrawApp main) {
    this.main = main;
  }

  public void add(Document document) {
    if (document != null) {
      curIndex = documents.size();
      curDocument = document;
      documents.add(document);
      showDocument(document);
    }
  }

  public void showDocument(int i) {
    if (i >= 0 && i < documents.size()) {
      Document doc = documents.get(i);
      if (doc != null) {
	curIndex = i;
	curDocument = doc;
	showDocument(doc);
      }
    }
  }

  public void closeDocument() {

  }

  public Document getCurrentDocument() {
    return curDocument;
  }

  public int findDocument(String name) {
    if (name != null) {
      int n = documents.size();
      for (int i = 0; i < n; i++) {
	Document doc = documents.get(i);
	if (doc != null && name.equals(doc.getName())) {
	  return i;
	}
      }
    }
    return -1;
  }

  protected void showDocument(Document document) {

  }

  protected DrawApp main;

  protected List<Document> documents = new ArrayList<Document>();

  protected Document curDocument = null;

  protected int curIndex = -1;

}
